<?php 
// +----------------------------------------------------------------------
// | Etion [ http://www.etion.cn ]
// +----------------------------------------------------------------------
// | File: Forder.class.php
// +----------------------------------------------------------------------
// | Date: 2010
// +----------------------------------------------------------------------
// | Author: 李汝康 <37739419@qq.com>
// +----------------------------------------------------------------------
// | 文件描述: 
// +----------------------------------------------------------------------
defined('IN') or die('Access Denied!');
/**
 * @name 订单模块
 *
 */
class ForderAction extends FbaseAction {
	
	/**
	 * content 表对象模型实例
	 * @var object
	 */
	protected $_c = '';
	
	/**
	 * @name初始化
	 */
	protected function _initialize() {
		parent::_initialize();
		//在此验证用户是否已登录，如未登录，则转到登陆页面
		
	}
	
	/**
	 * @name提交购物车物品，生成订单，等待用户确认
	 * 
	 */

	 public function show() {
		$in = &$this->in;	
		if (empty($_SESSION['userinfo'])) { //如果没有登录，就跳转
			$this->message(L ('您还没有登录，请先登录！'), __ROOT__ . '/index.html?m=findex&a=login');
		}
		$_user=D ("User");
		$userinfo=$_user->where("`user_id`=".$_SESSION['userinfo']['user_id'])->find();
		$this->assign('user',$userinfo);

		$sql="select * from `fangfa_content` as a, `fangfa_content_product` as b where a.cid=b.cid and a.cid=".$in['cid'];
		$query=mysql_query($sql);
		$data=mysql_fetch_array($query);
		$sql2="select `name`,`url` from `fangfa_category` where`catid`=".$data['catid'];

		$query2=mysql_query($sql2);
		$data2=mysql_fetch_array($query2);
		
		$seo['seotitle'] = $data['seotitle'];
		$seo['seokeywords'] = $data['seokeywords'];
		$seo['seodescription'] = $data['seodescription'];	
		$data['url']=$data2['url'].$data['url'];
		
		$cat['catid']=$data['catid'];
		$this->assign('cat',$cat);
		
		$this->assign('seo',$seo);
		$this->assign('type',$data2['name']);
		$this->assign('data',$data);
		$this->display('order/zxyd.html');
	}
	public function jump(){
		$in = &$this->in;
		if($in['type']==1){
			$this->message(L('I\'m sorry, payment time has expired!'), "__ROOT__/online.html");
		}
	}

	public function submit() {
		$in = &$this->in;
		if ($this->ispost()) {//用户提交订单
			if($in['ziyou']){   //自由行预定
				if (empty($_SESSION['cart'])) { //如果没有登录，就跳转
					$this->message(L ('您没有选择旅行计划，请先选择！'), __ROOT__ . '/index.html?m=fcart&a=show');
				}
				$addnum = 0;
				foreach ($_SESSION['cart'] as $k){
					$in['info']['ordername'][$addnum]['cid']=$k['cid'];	
					$in['info']['ordername'][$addnum]['pageurl']=$k['url'];
					$in['info']['ordername'][$addnum]['price']=$k['price'];
					$in['info']['ordername'][$addnum]['number']=$k['num'];
					if($k['catid']==31){
						$in['info']['ordername'][$addnum]['name']=$k['title']."（房型：".$k['fangxing']."）";
					}else{
						$in['info']['ordername'][$addnum]['name']=$k['title'];
					}
					$addnum++;
				}
				$in['info']['keyid']=1;
				
				$_user = D ('User');
				$where['user_id']=$_SESSION['userinfo']['user_id'];
				$us=$_user->where($where)->find();
				$in['info']['linkman']=$us['nickname'];
				$in['info']['mobile']=$us['telephone'];
				$in['info']['telephone']=$us['telephone'];
				$in['info']['email']=$us['email'];
				$in['info']['address']=$us['address'];
				$in['info']['postcode ']=$us['youbian'];
				
			}else{		//线路预定
				$in['info']['number'] = "成人：".intval($in['info']['adult'])." + 儿童：".intval($in['info']['child']);
				$in['info']['ordername'][0] = array(
				    'cid' => $in['info']['cid'] ? $in['info']['cid'] : 0,
				    'name' => $in['info']['title'],
				    'pageurl' => $in['info']['pageurl'] ? $in['info']['pageurl'] : '#',
				    'price' => $in['info']['price'] ? $in['info']['price'] : 0,
				    'number' => $in['info']['number'] ? $in['info']['number'] : 1,
				);
				//时间转换为时间戳
				$starting=explode("-",$in['info']['starting']);
				$in['info']['starting']=mktime(0,0,0.,$starting[1],$starting[2],$starting[0]);
	
				//把数组存如数据库中
				for($i=0;$i<count($in['info']['lvke']);$i++){
					if($in['info']['lvke'][$i]['name']==""){
						 unset($in['info']['lvke'][$i]);
					}
			    }
			 	$in['info']['lvke'] = var_export($in['info']['lvke'],"abc");  //把数组转换成字符串
			 	
			 	
			}							
			$_mOrder = D ('Order','admin');
			
			//生成订单号				
			$in['info']['ordernum'] = $_mOrder->getOrderNum($in['flag'] ? $in['flag'] : "F");
			$in['info']['userid'] = $_SESSION['userinfo']['user_id']; //此处为登录用户的用户ID号
			$in['info']['username'] = $_SESSION['userinfo']['username']; //此处为登录用户的用户名
			//print_r($in['info']['ordername']);
			$abc=$_mOrder->add($in['info']);
			if (false !== $abc) {  //生成订单信息，待用户确认提交
				//TODO
				if($in['ziyou']){
					$this->message(L($in['info']['linkman'].' 您好！订单提交成功，管理员会稍后和您取得联系！'), __ROOT__.'/index.html?m=forderto&a=manage&keyid=1');
				}else{
					$this->message(L($in['info']['linkman'].' 您好！订单提交成功，管理员会稍后和您取得联系！'), __ROOT__.'/index.html?m=forderto&a=manage&keyid=0');
				}
				
				tag('after_create_order_success');
			} else {
				$this->message(L('订单提交失败！') . '<br />' . $_mOrder->getError());
				tag('after_create_order_fail');
			}
		}
		$this->h404();
	}
	
	/**
	 * @name添加订单
	 *
	 */
	public function add(){
		$in = &$this->in;
		$this->_c = D ('Contentext');
		//获取数据
		$options = array(
			'where' => array('cid' => $in['cid']),
		);
		$data = $this->_c->get($options,'all');
		$category_data = F ( 'category_' . $data['catid'] );
		$data['pageurl'] = $category_data['catdir'].'/'.$data['url'];
		$data['forward'] = $this->forward;
		$this->assign('data',$data);
		//print_r($data);
		//print_r($in);
		$this->display('order/'.$in['a'].'.html');
	}
	
	/**
	 * @name确认订单
	 *
	 */
	public function confirm(){
		$in = &$this->in;
		$this->_c = D ('Contentext');
		//获取数据
		$options = array(
			'where' => array('cid' => $in['cid']),
		);
		$data = $this->_c->get($options,'all');
		$category_data = F ( 'category_' . $data['catid'] );
		$data['pageurl'] = $category_data['catdir'].'/'.$data['url'];
		$data['forward'] = $this->forward;
		$data['total'] = $data['price']*$in['info']['number'];
		$this->assign('data',$data);
		$this->assign('in',$in['info']);
		$this->display('order/'.$in['a'].'.html');
	}
}